// pages/sqmd/sqmd.js
import {mobileValidate} from '../../utils/util'
import {storeAdd, storeList, guideAdd, managerAdd} from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: [],
    name: '',
    address: '',
    phone: '',
    point: '',
    disabled: false,
    nameTip: false,
    phoneTip: false,
    regionTip: false,
    areaTip: false,
    storeId: '',
    storeList: [],
    storeTip: false,
    storeName: '',
    modalShow: false,
    items: [
      {value: '在线微信支付', name: '在线微信支付'},
      {value: '分期支付', name: '分期支付'},
      {value: '现金缴纳', name: '现金缴纳'},
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
  },

  getList() {
    storeList('').then(res => {
      this.setData({
        storeList: res
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  handleInput(e) {
    let value = e.detail.value
    let type = e.currentTarget.dataset.type
    this.setData({
      [type]: value,
      [type + 'Tip']: false
    })
    if (type == 'phone') {
      if (!mobileValidate(value)) {
        this.setData({
          phoneTip: true
        })
      }
    }
  },
  bindRegionChange(e) {
    this.setData({
      region: e.detail.value,
      regionTip: false
    })
  },
  bindLocationChange(e) {
    const that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              wx.showToast({ //这里提示失败原因
                title: '授权成功！',
                duration: 1500
              })
            },
            fail() {
              wx.showToast({
                title: '需要授权位置信息',
                icon: 'none'
              })
            }
          })
        } else {
          wx.chooseLocation({
            success(res) {
              console.log(res);
              that.setData({
                point: res.longitude + ',' + res.latitude,
                address: res.address,
                areaTip: false
              })
            },
            fail(res) {
              console.log(res);
            }
          })
        }
      }
    })
  },
  bindStoreChange(e) {
    this.setData({
      storeId: this.data.storeList[e.detail.value].id,
      storeName: this.data.storeList[e.detail.value].name,
      storeTip: false
    })
  },
  confirm() {
    this.setData({
      modalShow: true
    })
  },
  _confirmShop () {
    const {name, phone, region, point, address, disabled} = this.data
    if (name && phone && mobileValidate(phone) && region.length > 0 && point && address && !disabled) {
      this.setData({
        disabled: true
      })
      storeAdd({
        name,
        phone,
        province: region[0],
        city: region[1],
        area: region[2],
        point,
        address
      }).then(() => {
        wx.showToast({
          title: '操作成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1500)
      }).catch(() => {
        this.setData({
          disabled: false
        })
      })
    } else {
      this.setData({
        nameTip: !name,
        phoneTip: !phone || !mobileValidate(phone),
        regionTip: region.length == 0,
        areaTip: !address
      })
    }
  },
  _confirmTeacher () {
    const {name, phone, disabled, storeId} = this.data
    if (name && phone && mobileValidate(phone) && storeId && !disabled) {
      this.setData({
        disabled: true
      })
      managerAdd({
        name,
        phone,
        storeId
      }).then(() => {
        wx.showToast({
          title: '操作成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1500)
      }).catch(() => {
        this.setData({
          disabled: false
        })
      })
    } else {
      this.setData({
        nameTip: !name,
        phoneTip: !phone || !mobileValidate(phone),
        storeTip: !storeId
      })
    }
  },
  _confirmMd() {
    const {name, phone, disabled} = this.data
    if (name && phone && mobileValidate(phone) && !disabled) {
      this.setData({
        disabled: true
      })
      guideAdd({
        name,
        phone
      }).then(() => {
        wx.showToast({
          title: '操作成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1500)
      }).catch(() => {
        this.setData({
          disabled: false
        })
      })
    } else {
      this.setData({
        nameTip: !name,
        phoneTip: !phone || !mobileValidate(phone),
      })
    }
  },
  backPage() {
    wx.navigateBack()
  },
  agree() {
    wx.navigateTo({
      url: '/pages/rucruit/rucruit',
    })
  },
  radioChange(e) {
    console.log(e.detail.value)
  },
  cancel() {
    this.setData({
      modalShow: false
    })
  },
  confirm1() {},
})